/**************************************************************************//**
 * @item     CosyOS Kernel
 * @file     sv_int_psv.h
 * @brief    中断异步服务（仅在用户中断中调用，并在PendSV_Handler()中执行）
 * @author   迟凯峰
 * @version  V3.3.2
 * @date     2023.07.31
 ******************************************************************************/

#ifndef __SV_INT_PSV_H
#define __SV_INT_PSV_H



/*
 * 任务
 */

#if SYSCFG_TASKCREATEMODE == __STATIC__ || SYSCFG_TASKCREATEMODE == __BALANCE__
#define sISV_0x01(node) node
#define sISV_0x02(node) do{}while(false)
#elif SYSCFG_TASKCREATEMODE == __DYNAMIC__
#define sISV_0x01(node) NULL
#define sISV_0x02(node) isv_.task_node = node
#endif

/* 恢复任务 */
#define	sISV_ResumeTask(node, svid)	\
do{	\
	static tsISV_OpeTask isv_ = {__SVID_RESTASK__, sISV_0x01(node)};	\
	sISV_0x02(node);	\
	mISV_Do(svid);	\
}while(false)

/* 挂起任务 */
#define	sISV_SuspendTask(node, svid)	\
do{	\
	static tsISV_OpeTask isv_ = {__SVID_SUSTASK__, sISV_0x01(node)};	\
	sISV_0x02(node);	\
	mISV_Do(svid);	\
}while(false)

/* 删除任务 */
#define	sISV_DeleteTask(node, svid)	\
do{	\
	static tsISV_OpeTask isv_ = {__SVID_DELTASK__, sISV_0x01(node)};	\
	sISV_0x02(node);	\
	mISV_Do(svid);	\
}while(false)

/* 设置任务优先级 */
#define	sISV_SetPriority(node, tpl, svid)	\
do{	\
	static tsISV_SetTPL isv_ = {__SVID_SETTPL__, sISV_0x01(node), tpl};	\
	sISV_0x02(node);	\
	mISV_Do(svid);	\
}while(false)



/*
 * 延时
 */

/* 设置延时 */
#define	sISV_SetDelay(node, tc, svid)	\
do{	\
	static tsISV_SetDly isv_ = {__SVID_SETDLY__, sISV_0x01(node), tc};	\
	sISV_0x02(node);	\
	mISV_Do(svid);	\
}while(false)

/* 清除延时 */
#define	sISV_ClearDelay(node, svid)	\
do{	\
	static tsISV_OpeTask isv_ = {__SVID_CLRDLY__, sISV_0x01(node)};	\
	sISV_0x02(node);	\
	mISV_Do(svid);	\
}while(false)



/*
 * 定时
 */

/* 定时中断 */
#define	sISV_TimInt(tmid, tc, svid)	\
do{	\
	static tsISV_TimInt isv_ = {__SVID_TIMINT__, tmid, tc};	\
	mISV_Do(svid);	\
}while(false)

/* 定时查询 */
#define	sISV_TimQry(tmid, tc, svid)	\
do{	\
	static tsISV_TimQry isv_ = {__SVID_TIMQRY__, tmid, tc};	\
	mISV_Do(svid);	\
}while(false)



/*
 * 二值信号量
 */

/* 释放/上锁 */
#define sISV_WriteBin(bin, val, svid)	\
do{	\
	static tsISV_Bin isv_ = {__SVID_WRITEBIN__, (bool *)&bin, val};	\
	mISV_Do(svid);	\
}while(false)



/*
 * 计数信号量
 */

/* 释放 */
#define sISV_FreeSem(sem, svid)	\
do{	\
	static tsISV_Sem isv_ = {__SVID_FREESEM__, &sem};	\
	mISV_Do(svid);	\
}while(false)



/*
 * 飞信
 */

/* 发送飞信 */
#define sISV_SendFetion(tbox, tion, svid)	\
do{	\
	static tsISV_Fetion isv_ = {__SVID_SENDFETION__, (tFetion *)&tbox, 0};	\
	isv_.fetion = tion;	\
	mISV_Do(svid);	\
}while(false)



/*
 * 消息邮箱
 */

#if SYSCFG_INTRECVMAIL == 0
#define sISV_0x03
#elif SYSCFG_INTRECVMAIL == 1
#define sISV_0x03 true,
#endif

/* 发送邮件 */
#define sISV_SendMail(mbox, mail, svid)	\
do{	\
	static tsMbxNode_ISV mbx_node = {&mbox, mail, sISV_0x03 &mbox##_gnmf, sizeof(mbox)};	\
	static tsISV_Mbx isv_ = {__SVID_SENDMAIL__, &mbx_node};	\
	mISV_Do(svid);	\
}while(false)



/*
 * 消息队列
 */

/* 发送消息 */
#define	sISV_SendMsg(que, msg, svid)	\
do{	\
	static tsISV_Msg isv_ = {__SVID_SENDMSG__, que, msg};	\
	mISV_Do(svid);	\
}while(false)



/*
 * 事件标志组
 */

/* 清除标志组 */
#define sISV_ClearFlagGroup(group, svid)	\
do{	\
	static tsISV_GRP isv_ = {__SVID_WRITEGRP__, (void *)&group, -sizeof(group), ~0};	\
	mISV_Do(svid);	\
}while(false)

/* 写多标志位 */
#define sISV_WriteFlagBits(group, sign, nbit, svid)	\
do{	\
	static tsISV_GRP isv_ = {__SVID_WRITEGRP__, (void *)&group, sign##sizeof(group), 0};	\
	ts_##group lv;	\
	miWriteFlagBits;	\
	sizeof(lv) == 1 ? *(u8  *)&lv = false : OS_NOPx1;	\
	sizeof(lv) == 2 ? *(u16 *)&lv = false : OS_NOPx1;	\
	sizeof(lv) == 4 ? *(u32 *)&lv = false : OS_NOPx1;	\
	siWriteFlagBits_##nbit(svid, 



/*
 * 全局变量
 */

/* 全局变量写访问 */
#define sISV_WriteGVar(gp, lp, size, svid)	\
do{	\
	static tsVarNode_ISV var_node = {gp, lp, size};	\
	static tsISV_GVar isv_ = {__SVID_WRITEVAR__, &var_node};	\
	mISV_Do(svid);	\
}while(false)

/* 全局变量自运算 */
#define sISV_SelfOpe(gv, type, fp, svid)	\
do{	\
	static type lv;	\
	static tsVarNode_ISV var_node = {&gv, &lv, sizeof(gv)};	\
	static tsISV_SelfOpe isv_ = {__SVID_SELFOPE__, &var_node, fp};	\
	mISV_Do(svid);	\
}while(false)



#endif
